//
//  VisitorLoginView.swift
//  WeiboTest
//
//  Created by 张文文 on 15/5/12.
//  Copyright (c) 2015年 zww. All rights reserved.
//

import UIKit
///NSObjectProtocol 基协议   所有继承自这个协议，都可以实现协议
protocol VisitorLoginViewDelegate: NSObjectProtocol {
    //1.协议方法
    
    //2.点击登陆按钮
    func visitorViewLoginViewDidSelected()
    //3.点击注册按钮
    func visitorViewRegisterViewDidSelected()
}

class VisitorLoginView: UIView {

    @IBOutlet weak var smallIconView: UIImageView!
    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var messageLabel: UILabel!
    
    //定义代理(防止循环引用用weak)
    weak var delegate: VisitorLoginViewDelegate?
    
    func setupInfo(imageName: String, message: String ,isHome : Bool = false) {

        //隐藏图片视图
        iconView.hidden = !isHome
        if isHome{
            //若是首页，图片视图加载图片
            iconView.image = UIImage(named: imageName)
            startAnimation()
            
        }
        else{
            //若不是首页，隐藏图片视图，对转动视图重新赋值（在转动视图加载相应的图片）
            smallIconView.image = UIImage(named: imageName)
            stopAnimation()
        }
        messageLabel.text = message
        
        
    }
    ///开始动画
    func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20.0
        
        smallIconView.layer.addAnimation(anim, forKey: nil)
        
    }
    ///停止动画
    func stopAnimation() {
    
        smallIconView.layer.removeAllAnimations()
    }
    
    @IBAction func register() {
        delegate?.visitorViewRegisterViewDidSelected()
        
    }
    @IBAction func login() {
        delegate?.visitorViewLoginViewDidSelected()
    }
}
